//
//  QSSubmitProcessingViewController.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/3/27.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSSubmitProcessingViewController.h"
#import "QSSubmitProcessingCell.h"
#import "QSProblemInfoCell.h"
#import "QSProblemReprotInterface.h"
#import "QSProblemProcessingInterface.h"
#import "QSProblemDetailViewController.h"
#import "QSProblemProcessingViewController.h"
@interface QSSubmitProcessingViewController ()
/** 确认完成Button*/
@property (nonatomic, strong)UIButton *completeButton;

@property (nonatomic, strong)QSProblemDetailModel *detailModel;

@property (nonatomic, copy)NSString *remark;
@end
static NSString *submitProcessingCell = @"SubmitProcessingCell";
static NSString *problemInfoCell = @"ProblemInfoCell";
@implementation QSSubmitProcessingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"问题处理";
    [self configSubView];
    [self getProblemDetail];
}


- (void)configSubView{
  self.tableView.estimatedRowHeight = 40;
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavgationBarHeight - kRealValue(78));
    [self.tableView registerNib:[UINib nibWithNibName:@"QSSubmitProcessingCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:submitProcessingCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"QSProblemInfoCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:problemInfoCell];
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#F1F3F4"];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F1F3F4"];
    
    [self.view addSubview:self.completeButton];
    [_completeButton mas_makeConstraints:^(MASConstraintMaker *make) {
           make.left.equalTo(self.view).offset(kRealValue(24));
           make.right.equalTo(self.view).offset(- kRealValue(24));
           make.bottom.equalTo(self.view).offset(- kRealValue(24));
           make.height.offset(44);
    }];
}


- (UIButton *)completeButton{
    if (!_completeButton) {
           _completeButton = [UIButton buttonWithType:UIButtonTypeCustom];
           _completeButton.layer.cornerRadius = 22;
           _completeButton.backgroundColor = [UIColor colorWithHexString:@"#128BEC"];
           [_completeButton setTitle:@"确认完成" forState:UIControlStateNormal];
           [_completeButton setTitleColor:[UIColor QSColorWhiteFFFFFF] forState:UIControlStateNormal];
           _completeButton.titleLabel.font = [UIFont QSFontOfSize16];
           [_completeButton addTarget:self action:@selector(completeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
       }
       return _completeButton;
}

- (void)getProblemDetail{
    [QSProblemReprotInterface getApiProblemDetailWithID:self.problemId.integerValue andBlock:^(ResponseMessage * _Nonnull rspStatusAndMessage, QSProblemDetailModel * _Nonnull result) {
        if (rspStatusAndMessage.code == kResponseSuccessCode) {
            self.detailModel = result;
            [self.tableView reloadData];
//            if (result.state.integerValue == 2) {
//            }else{
//                QSProblemDetailViewController *vc = [QSProblemDetailViewController new];
//                vc.problemId = result.id;
//                [self.navigationController pushViewController:vc animated:YES];
//            }
        }
    }];
}

#pragma mark - **************** UITableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  if (!self.detailModel) {
    return 0;
  }
  return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.row == 0) {
            QSSubmitProcessingCell *cell = [tableView dequeueReusableCellWithIdentifier:submitProcessingCell];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.detailModel = self.detailModel;
            cell.didInputDealRemarkFinishBlock = ^(NSString * _Nonnull remark) {
                self.remark = remark;
            };
            return cell;
        }else{
            QSProblemInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:problemInfoCell];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.model = self.detailModel;
            return cell;
        }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.row == 0) {
//        return [tableView cellHeightForIndexPath:indexPath model:self.detailModel keyPath:@"detailModel" cellClass:[QSSubmitProcessingCell class] contentViewWidth:kScreenWidth - 20];
//    }else{
//        return [tableView cellHeightForIndexPath:indexPath model:self.detailModel keyPath:@"model" cellClass:[QSProblemInfoCell class] contentViewWidth:kScreenWidth - 20];//cell间距
//    }
  return UITableViewAutomaticDimension;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
- (void)completeButtonAction:(UIButton *)sender{
    if (self.remark.length == 0) {
        [QSAppKeyWindow showAutoHideHudWithText:@"请输入处理信息"];
        return;
    }
    WeakSelf(weakSelf);
    [QSAppKeyWindow showIndeterminateHudWithText:@"请稍候"];
    [QSProblemProcessingInterface postApiProblemDealWithId:_detailModel.id.integerValue Info:self.remark andBlock:^(ResponseMessage * _Nonnull rspStatusAndMessage, NSDictionary * _Nonnull result) {
        [QSAppKeyWindow hideHud];
        if (rspStatusAndMessage.code == kResponseSuccessCode) {
            if ([result[@"type"] integerValue] == 1) {//未被处理过得问题
                [QSAppKeyWindow showAutoHideHudWithText:@"处理成功!"];
                if (self.isMessagePush) {//是从首页消息也进来的 跳转已处理
                    QSProblemProcessingViewController *vc = [QSProblemProcessingViewController new];
                    vc.isDealFinish = YES;
                    [self.navigationController pushViewController:vc animated:YES];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [vc.magicController.magicView switchToPage:1 animated:YES];
                    });
                }else{
                    [self backIsDeal];//返回已处理
                }
            }else{
                //已被处理过得问题
                [QSAppKeyWindow showAutoHideHudWithText:rspStatusAndMessage.message];
                [self.navigationController popViewControllerAnimated:YES];
            }
            
        }
    }];

}
//返回已处理
- (void)backIsDeal{
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:[QSProblemProcessingViewController class]]) {
            QSProblemProcessingViewController *vc = (QSProblemProcessingViewController *)controller;
            [vc.magicController switchToPage:1 animated:YES];
            [self.navigationController popToViewController:vc animated:YES];
            break;
        }
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
    [self.view endEditing:YES];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if (self.tableView) {
        [self tableViewShouldUpdateDataByPageIndex:1];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
